﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using WeifenLuo.WinFormsUI.Docking;
using System.Reflection;
using WeifenLuo.WinFormsUI.Docking;
using WHC.Security.Entity;
using WHC.Security.DALSQL;


namespace CKNetFrameWork
{
    public partial class Frm_Main : Form
    {
        private Frm_MainToolWindow mainToolWin = null;
        private static Frm_Main m_Frm_Main = null;
   
        public static Dictionary<string, Dictionary<string, string>> m_dicFormText2FormTypeName = new Dictionary<string, Dictionary<string, string>>();
        private List<FunctionInfo> m_lstOneLevelFunRuntime = new List<FunctionInfo>();
        private WHC.Security.BLL.Function function = new WHC.Security.BLL.Function();
        public static int m_nLoginUserId = 0;
        public Frm_Main()
        {
            InitializeComponent();
            InitFormText2FormType();
            InitFirstLevelFun();
            InitSecondLevelFun();
   
        }
        private void InitFormText2FormType()
        {
            Dictionary<string, string> dicCSharpBase = new Dictionary<string, string>();
            dicCSharpBase.Add("产品管理", "CKNetFrameWork.Frm_Product");
            dicCSharpBase.Add("区域管理", "CKNetFrameWork.Frm_Region");
            dicCSharpBase.Add("代理商管理", "CKNetFrameWork.Frm_Agent");
            dicCSharpBase.Add("零售商管理", "CKNetFrameWork.Frm_Seller");
            m_dicFormText2FormTypeName.Add("资料管理", dicCSharpBase);

            Dictionary<string, string> dicCSharpStore = new Dictionary<string, string>();
            dicCSharpStore.Add("充值管理", "CKNetFrameWork.Frm_Deposit");
            dicCSharpStore.Add("入库管理", "CKNetFrameWork.Frm_InStore");
            m_dicFormText2FormTypeName.Add("充值入库管理", dicCSharpStore);
        }
        //初始化一次每次新增模块时调用
        private void InitFirstLevelFun()
        {
 
            List<FunctionInfo> list = WHC.Security.BLL.BLLFactory<WHC.Security.BLL.Function>.Instance.GetAllFirstLevelFunctions();
            List<string> listName = new List<string>();
            foreach(FunctionInfo f in list)
            {
                listName.Add(f.Name);
            }
            foreach (string strTemp in m_dicFormText2FormTypeName.Keys)
            {
                if (listName.Contains(strTemp))
                    continue;
                FunctionInfo info = new FunctionInfo();
                info.Name = strTemp;
                info.PID = -1;
                info.ControlID = "";
                info.SystemType_ID = "WareMis";
                function.Insert(info);
            }
        }
        //初始化一次每次新增模块时调用
        private void InitSecondLevelFun()
        {


            List<FunctionInfo> list = WHC.Security.BLL.BLLFactory<WHC.Security.BLL.Function>.Instance.GetAllFunctions();
            List<string> listName = new List<string>();
            foreach (FunctionInfo f in list)
            {
                listName.Add(f.Name);
            }

            foreach (string strModule in Frm_Main.m_dicFormText2FormTypeName.Keys)
            {
                FunctionInfo fi = function.FindSingle("name='" + strModule+"'");
                foreach (string strItem in Frm_Main.m_dicFormText2FormTypeName[strModule].Keys)
                {
                    if (listName.Contains(strItem))
                        continue;
                    FunctionInfo info = new FunctionInfo();
                    info.Name = strItem;
                    info.PID = fi.ID;
                    info.ControlID = Frm_Main.m_dicFormText2FormTypeName[strModule][strItem];
                    info.SystemType_ID = "WareMis";
                    function.Insert(info);
                }
            }
        }
        private string GetParentName(FunctionInfo functionInfoChild)
        {
            foreach (FunctionInfo functionInfo in m_lstOneLevelFunRuntime)
            {
                if (functionInfo.ID == functionInfoChild.PID)
                {
                    return functionInfo.Name;
                }
            }
            return "";
        }
        private void InitFormText2FormTypeNameDic()
        {
            m_lstOneLevelFunRuntime.Clear();
            m_dicFormText2FormTypeName.Clear();
            List<FunctionInfo> list = WHC.Security.BLL.BLLFactory<WHC.Security.BLL.Function>.Instance.GetFunctionsByUser(m_nLoginUserId, "WareMis");
         
            if (list != null && list.Count > 0)
            {
             
                foreach (FunctionInfo functionInfo in list)
                {
                    if (functionInfo.PID == -1)
                    {
                        Dictionary<string, string> dicCSharpBase = new Dictionary<string, string>();
                        m_dicFormText2FormTypeName.Add(functionInfo.Name, dicCSharpBase);
                        m_lstOneLevelFunRuntime.Add(functionInfo);
                    }
                }
                foreach (FunctionInfo functionInfoFather in m_lstOneLevelFunRuntime)
                {
                    foreach (FunctionInfo functionInfo in list)
                    {
                        if (functionInfo.PID == functionInfoFather.ID)
                        {
                            //string strTemp = GetParentName(functionInfo);
                            if (m_dicFormText2FormTypeName.Keys.Contains(functionInfoFather.Name))
                                m_dicFormText2FormTypeName[functionInfoFather.Name].Add(functionInfo.Name, functionInfo.ControlID);
                               

                        }
                    }
                }

            }

            return;
           
            
        }
        public static Frm_Main GetMainForm()
        {
            if (m_Frm_Main == null)
            {
                m_Frm_Main = new Frm_Main();
            }

            return m_Frm_Main;
        }
        private DockContent FindDocument(string text)
        {
            if (dockPanel.DocumentStyle == DocumentStyle.DockingMdi)
            {
                foreach (Form form in MdiChildren)
                {
                    if (form.Text == text)
                    {
                        return form as DockContent;
                    }
                }

                return null;
            }
            else
            {
                foreach (DockContent content in dockPanel.Documents)
                {
                    if (content.DockHandler.TabText == text)
                    {
                        return content;
                    }
                }

                return null;
            }
        }

        public void ShowContent(DockContent frm)
        {
            frm.Show(this.dockPanel);
            frm.Focus();
            frm.BringToFront();
        }
        private string GetClassByName(string strName)
        {
            string strTemp = "";
            foreach (string strModule in Frm_Main.m_dicFormText2FormTypeName.Keys)
            {
                foreach (string strItem in Frm_Main.m_dicFormText2FormTypeName[strModule].Keys)
                {
                    if (strItem == strName)
                    {
                        strTemp = Frm_Main.m_dicFormText2FormTypeName[strModule][strItem];
                        break;
                    }
                }
            }
            return strTemp;
        }

        
        public DockContent ShowContent(string caption/*, Type formType*/)
        {

            DockContent frm = FindDocument(caption);

            if (frm == null)
            {
                //DockContent currentMdiChild = null;
                //Assembly assembly = Assembly.GetExecutingAssembly();
                //if (!m_dicFormText2FormTypeName.ContainsKey(caption))
                //    return frm;
                //Type typForm = assembly.GetType(m_dicFormText2FormTypeName[caption]);
                string strTemp = GetClassByName(caption);
                if (strTemp == "")
                    return null;

                string[] strSplit = strTemp.Split('.');
                string strAssemblyName = strSplit[0];

                Assembly assembly = Assembly.Load(strAssemblyName);
                //test(assembly);

                Type typForm = assembly.GetType(strTemp);
                //关于InvokeMember用法，不明白的可以查看：http://msdn.microsoft.com/zh-cn/library/de3dhzwy(VS.80).aspx
                Object mdiChild = typForm.InvokeMember(
                   null,
                    BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance,
                   null,
                   null,
                   null);

                if (mdiChild != null)
                {
                    frm = mdiChild as DockContent;
                    frm.MdiParent = this;
                    frm.WindowState = FormWindowState.Maximized;

                }
            }

            frm.Show(this.dockPanel);
            frm.Focus();
            frm.BringToFront();
            return frm;
        }
        private void Frm_Main_Load(object sender, EventArgs e)
        {
            try
            {
                
   
                Frm_Login frm_login = new Frm_Login();

                if (frm_login.ShowDialog() == DialogResult.OK)
                {
                    int a = 0;
                }
                else
                {
                    int b = 0;
                    Application.Exit();
                    return;
                }
                InitFormText2FormTypeNameDic();
                if (Frm_Main.m_dicFormText2FormTypeName.Count != 0)
                {
                    mainToolWin = new Frm_MainToolWindow();
                    mainToolWin.Show(this.dockPanel, DockState.DockLeft);
                }

                

                this.dockPanel.DockLeftPortion = 0.17;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return ;
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            WHC.Security.UI.Portal.StartLogin();
        }
    }
}
